export default {
  setData(ctx) {
    //获取localStorage里面的属性和数据
    let phoneData = JSON.parse(localStorage.getItem('phoneData') || '[]');
    const totalPrice = Number(localStorage.getItem('totalPrice') || '0'),
      totalMount = Number(localStorage.getItem('totalMount') || '0'),
      cartData = JSON.parse(localStorage.getItem('cartData') || '[]');

    //处理phoneData底下pics属性里保存的数组数据
    phoneData = phoneData.map((item) => {
      //phoneData: [pics:"[{0:["https:xxxx.com/xxx"]}]"]
      const picsData = JSON.parse(item.pics);
      item.img = picsData[0][0][0];
      return item;
    });

    //commit给mutations调用底下的SET_DATA方法
    //传入组装过的对象
    ctx.commit('SET_DATA', {
      phoneData,
      totalPrice,
      totalMount,
      cartData
    });
  },
  /**
   * @param {*} payload {price,type}
   */
  setTotal(ctx, payload) {
    ctx.commit('SET_TOTAL', payload);
  },
  /**
   * @param {*} payload {type,id,img,name,price,slogan}
   */
  setCart(ctx, payload) {
    ctx.commit('SET_CART', payload);
  }
}